#include "myswitchbotton.h"
#include <QPainter>

mySwitchBotton::mySwitchBotton(QWidget *parent) :
    QWidget(parent)
{
    curX = height() / 2;

    animation = new QPropertyAnimation(this);
    animation->setDirection(QVariantAnimation::Forward);
    animation->setTargetObject(this);
    animation->setDuration(50);
    //    animation->setEasingCurve((QEasingCurve::InOutElastic));

    connect(animation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value)
    {
        curX = value.toInt();
        update();
    });
}

QColor mySwitchBotton::offBgColor() const
{
    return m_offBgColor;
}

void mySwitchBotton::setOffBgColor(const QColor &newOffBgColor)
{
    if (m_offBgColor == newOffBgColor)
        return;
    m_offBgColor = newOffBgColor;
    update();
    emit offBgColorChanged();
}

QColor mySwitchBotton::onBgColor() const
{
    return m_onBgColor;
}

void mySwitchBotton::setOnBgColor(const QColor &newOnBgColor)
{
    if (m_onBgColor == newOnBgColor)
        return;
    m_onBgColor = newOnBgColor;
    update();
    emit onBgColorChanged();
}

QColor mySwitchBotton::offIndicatorColor() const
{
    return m_offIndicatorColor;
}

void mySwitchBotton::setOffIndicatorColor(const QColor &newOffIndicatorColor)
{
    if (m_offIndicatorColor == newOffIndicatorColor)
        return;
    m_offIndicatorColor = newOffIndicatorColor;
    emit offIndicatorColorChanged();
}

QColor mySwitchBotton::onIndicatorColor() const
{
    return m_onIndicatorColor;
}

void mySwitchBotton::setOnIndicatorColor(const QColor &newOnIndicatorColor)
{
    if (m_onIndicatorColor == newOnIndicatorColor)
        return;
    m_onIndicatorColor = newOnIndicatorColor;
    emit onIndicatorColorChanged();
}

QColor mySwitchBotton::disableColor() const
{
    return m_disableColor;
}

void mySwitchBotton::setDisableColor(const QColor &newDisableColor)
{
    if (m_disableColor == newDisableColor)
        return;
    m_disableColor = newDisableColor;
    update();
    emit disableColorChanged();
}

QString mySwitchBotton::offText() const
{
    return m_offText;
}

void mySwitchBotton::setOffText(const QString &newOffText)
{
    if (m_offText == newOffText)
        return;
    m_offText = newOffText;
    update();
    emit offTextChanged();
}

QString mySwitchBotton::onText() const
{
    return m_onText;
}

void mySwitchBotton::setOnText(const QString &newOnText)
{
    if (m_onText == newOnText)
        return;
    m_onText = newOnText;
    update();
    emit onTextChanged();
}

QString mySwitchBotton::disableText() const
{
    return m_disableText;
}

void mySwitchBotton::setDisableText(const QString &newDisableText)
{
    if (m_disableText == newDisableText)
        return;
    m_disableText = newDisableText;
    update();
    emit disableTextChanged();
}

bool mySwitchBotton::isOff() const
{
    return m_isOff;
}

void mySwitchBotton::setIsOff(bool newIsOff)
{
    if (m_isOff == newIsOff)
        return;
    m_isOff ? animation->setDirection(QVariantAnimation::Forward)
            : animation->setDirection(QVariantAnimation::Backward);
    animation->start();
    m_isOff = newIsOff;
    update();
    emit isOffChanged();
}

bool mySwitchBotton::isEnable() const
{
    return m_isEnable;
}

void mySwitchBotton::setIsEnable(bool newIsEnable)
{
    if (m_isEnable == newIsEnable)
        return;
    m_isEnable = newIsEnable;
    update();
    emit isEnableChanged();
}

void mySwitchBotton::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);
    painter.setRenderHint(QPainter::Antialiasing, true);

    painter.setPen(Qt::NoPen);
    painter.setBrush(m_isOff ? m_offBgColor : m_onBgColor);
    painter.drawRoundedRect(this->rect(), height() / 2, height() / 2);

    painter.setBrush(m_isOff ? m_offIndicatorColor : m_onIndicatorColor);
    painter.drawEllipse(QPoint(curX, height() / 2), height() / 2 - height() / 20, height() / 2 - height() / 20);

    painter.setPen(Qt::white);
    painter.setFont(QFont("得意黑", (float)height() * 0.3));
    painter.drawText((float)width() * (m_isOff ? 0.55 : 0.2), (float)height() * 0.64, m_isOff ? m_offText : m_onText);
}

void mySwitchBotton::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_isOff ? animation->setDirection(QVariantAnimation::Forward)
              : animation->setDirection(QVariantAnimation::Backward);
        m_isOff = !m_isOff;
        animation->start();
    }
}

void mySwitchBotton::resizeEvent(QResizeEvent *event)
{
    Q_UNUSED(event);
    animation->setStartValue(height() / 2);
    animation->setEndValue(width() - (height() / 2));
}
